#coding: utf-8 or # -*- coding: utf-8 -*-
import os
import json
from flask import Flask
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import (create_engine, MetaData, Column, Integer, String)
from sqlalchemy import func
from sqlalchemy.sql.expression import and_, or_
from sqlalchemy import Table, Column, Integer, String, Text, DateTime, MetaData, ForeignKey, Boolean, types, Numeric
from datetime import datetime, date,time
from sqlca import get_Session
from json_exa import ret_json, row3dict
from login import infor_worker
from get_cardinfo import card_ticket_sum,parse_track
from pos_check import token_check,pos_en
import sys
from card_app_log import app_log


reload(sys)
sys.setdefaultencoding('utf8')


Base = declarative_base()
class T_LZ_CARD_SALE_DAY(Base):
    __tablename__ = 'T_LZ_CARD_SALE_DAY'
    C_DT = Column(String(10))
    D_RETAIL_TIME = Column(DateTime, primary_key=True)
    C_SHOP_ID = Column(String(10), primary_key=True)
    C_POS_ID = Column(String(10), primary_key=True)
    C_CASHIER_ID = Column(String(10), primary_key=True)
    C_RETAIL_ID = Column(String(20), primary_key=True)
    C_CARD_ID = Column(String(30), primary_key=True)
    C_TYPE = Column(String(1), primary_key=True)
    C_GATHER_ID = Column(String(3), primary_key=True)
    C_GATHER_NAME = Column(String(10))
    N_MONEY = Column(Numeric(18, 2))
    C_TRANS_FLAG = Column(String(2))
    N_MONEY_BALANCE = Column(Numeric(18, 2))
    C_ACT_ID = Column(String(20))
    D_CON_BEGIN = Column(DateTime)
    D_CON_END = Column(DateTime)
    C_PROCESS_FLAG = Column(String(3))

class T_LZ_SALE_APP(Base):
	__tablename__ = 'T_LZ_SALE_APP'
	bill = Column(String(20))
	C_DT = Column(String(20))
	dt = Column(DateTime,primary_key=True)
	cashier_id = Column(String(30))
	cashier = Column(String(30))
	CARD_ID = Column(String(30),primary_key=True)
	type = Column(String(1))
	GATHER_ID = Column(String(3))
	GATHER_NAME = Column(String(10))
	MONEY = Column(Numeric(18, 2))
	posno  = Column(String(20))
	N_MONEY_BALANCE = Column(Numeric(18, 2))
		


		

def sale_query(data_list):
	app_log(data_list,'input')
	list_w = data_list.split('&')
	print list_w
	json_list = {}
	for lst in list_w:
	    li = lst.split('=',1)
	    json_list[li[0]] = li[1]
	token_c = token_check(json_list['key'])
	print token_c
	if token_c['code'] != '10':
	    return json.dumps(token_c, ensure_ascii=False)
	oper = token_c['operator']
	posen = pos_en(json_list['posno'])

	if posen['code'] != '10':
	    return json.dumps(posen, ensure_ascii=False)
	ls_dt = json_list['dt']
	session = get_Session()()
	newdt = session.execute("select to_char(sysdate,'yyyy-mm-dd') from DUAL").first()[0]   
	print json_list['posno']
	print ls_dt
	if len(ls_dt) != 8:
		ls_msg = '格式错误正确格式为yyyymmdd'
		session.close()
		return json.dumps({"code": "81", "error": ls_msg },ensure_ascii=False)
	ls_dt = session.execute("select to_char( to_date(:ls_dt,'yyyy-mm-dd'),'yyyy-mm-dd') from DUAL",{"ls_dt":ls_dt}).first()[0]
	print ls_dt   
	query = session.query(T_LZ_SALE_APP).filter(T_LZ_SALE_APP.posno==json_list['posno'] , T_LZ_SALE_APP.C_DT  == ls_dt ).all()
	# for i in query:
	# 	print i.dt
	dt_s = []
	for u in query:
		dt_s.append(row3dict(u))
	sale_list =  dt_s
	print sale_list
	ls_msg = 'succeed'
	session.close()
	return  json.dumps({"code": "10", "error": ls_msg,"data":sale_list },ensure_ascii=False)
   





# session = get_Session()()
# # query = session.query(T_LZ_SALE_APP).filter(T_LZ_SALE_APP.posno=='7112' , T_LZ_SALE_APP.C_DT  == '2015-10-08' ).all()
# query = session.query(T_LZ_SALE_APP).filter(T_LZ_SALE_APP.posno=='7112' , T_LZ_SALE_APP.C_DT  == '2016-01-28' ).all()
# card_balance =  ret_json(query)
# print card_balance
# dtime = session.execute("select (sysdate) from DUAL").first()[0]
# newdt = str(dtime) 
# print newdt

# ls_dt = '20160128'
# ls_dt = session.execute("select to_char( to_date(:ls_dt,'yyyy-mm-dd'),'yyyy-mm-dd') from DUAL",{"ls_dt":ls_dt}).first()[0]   
# print ls_dt 

# session = get_Session()()
# query = session.query(T_LZ_SALE_APP).filter(T_LZ_SALE_APP.posno=='7102' , T_LZ_SALE_APP.C_DT  == '2016-01-28' ).all()
	
# dt_s = []
# for u in query:
# 	dt_s.append(row3dict(u))
# 	print dt_s
# ls_str = 'req_type=sale&posno=7102&key=867<25626=$53=34=2;.9213-70035&dt=20160128'
# print sale_query(ls_str)

# ls_dt = '2016130'
# if len(ls_dt) != 8:
# 	print '33'

